<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\LotteryType;

/**
 * 玩法逻辑
 */
class LotteryPlay extends AdminBase
{
    

    
    /**
     * 获取玩法列表
     */
    public function getLotteryPlayList($where = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {

        $play_cate=$this->modelLotteryPlayType->getList(['status'=>1,'is_old'=>$where['a.is_old']],'*','id desc',false);
        $count=0;
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        foreach ($play_cate as $k=>$v){
            $where['a.play_cate_id']=$v['id'];
            $play_cate[$k]['list']=$this->modelLotteryPlay->alias('a')->field('a.*')->join('__LOTTERY_TYPE__ c', 'a.cate_id = c.id')->where($where)->select();
            if(!$play_cate[$k]['list']){
                unset($play_cate[$k]);
            }else{
                $play_cate[$k]['count']=$this->modelLotteryPlay->alias('a')->field('a.*')->join('__LOTTERY_TYPE__ c', 'a.cate_id = c.id')->where($where)->count();
                $count+=$play_cate[$k]['count'];
            }

        }


        return ['list'=>$play_cate,'count'=>$count];
    }
    
    /**
     * 获取玩法列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describle'] = ['like', '%'.$data['search_data'].'%'];
        if(!empty($data['cate_id'])){
            $where['a.cate_id'] =$data['cate_id'];
        }else{
            $data['cate_id']=LotteryType::where(['status'=>1])->value('id');
            $where['a.cate_id'] =$data['cate_id'];
        }

        return $where;
    }
    
    /**
     * 玩法信息编辑
     */
    public function LotteryPlayEdit($data = [])
    {
        if($data['is_old']==0) {
            $validate_result = $this->validateLotteryPlay->scene('edit')->check($data);

            if (!$validate_result) {

                return [RESULT_ERROR, $this->validateLotteryPlay->getError()];
            }
        }
        if($data['is_old']==1){
            $url = url('OldLotteryPlayList');
        }else{
            $url = url('LotteryPlayList');
        }


        $result = $this->modelLotteryPlay->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '玩法' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '玩法操作成功', $url] : [RESULT_ERROR, $this->modelLotteryPlay->getError()];
    }



    /**
     * 获取玩法分类列表
     */
    public function getLotteryPlayCateList($where = [])
    {



        $where[ DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $play_cate=$this->modelLotteryPlayType->getList($where,'*','id desc');
        $count=$this->modelLotteryPlayType->where($where)->count();
        return ['list'=>$play_cate,'count'=>$count];
    }

    /**
     * 玩法分类信息编辑
     */
    public function LotteryPlayCateEdit($data = [])
    {


        $url = url('LotteryPlayCateList');




        $result = $this->modelLotteryPlayType->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '玩法分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '玩法分类操作成功', $url] : [RESULT_ERROR, $this->modelLotteryPlayType->getError()];
    }

    /**
     * 获取玩法信息
     */
    public function getLotteryPlayInfo($where = [], $field = 'a.*,c.name as category_name')
    {
        
        $this->modelLotteryPlay->alias('a');

        $join = [

            [ '__LOTTERY_TYPE__ c', 'a.cate_id = c.id'],
        ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelLotteryPlay->join = $join;
        
        return $this->modelLotteryPlay->getInfo($where, $field);
    }





    /**
     * 获取玩法分类信息
     */
    public function getLotteryPlayCateInfo($where = [], $field = 'a.*')
    {

        $this->modelLotteryPlayType->alias('a');



        return $this->modelLotteryPlayType->getInfo($where, $field);
    }



    /**
     * 玩法删除
     */
    public function LotteryPlayDel($where = [])
    {
        
        $result = $this->modelLotteryPlay->deleteInfo($where);
        
        $result && action_log('删除', '玩法删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '玩法删除成功'] : [RESULT_ERROR, $this->modelLotteryPlay->getError()];
    }

    /**
     * 玩法删除
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelLotteryPlay->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }
}
